﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Xml.Serialization;
using System.Xml.XPath;
using System.IO;

namespace Freecart_Local_.Classes
{
    public struct CartRow
    {
        public int ID;
        public int Quantity;
        public string Comment;
        public double Price;

        public CartRow(int ID, int Quantity, string Comment, double Price)
        {
            this.ID = ID;
            this.Quantity = Quantity;
            this.Comment = Comment;
            this.Price = Price;
        }
    }


    public class Cart
    {
        private Guid _guid;
        public Guid Guid { set { _guid = value; } get { return this._guid; } }

        private CartItemsCollector _items;
        public CartItemsCollector Items { private set { this._items = value;} get { return this._items; } }

        public Cart()
        {
            this._guid = new Guid();
            this._items = new CartItemsCollector();

            Generate _create = new Generate();

            foreach (DataColumn datacolumn in _create.Columns())
            {
                this._items.Table.Columns.Add(datacolumn);
            }

            this._items.Table.TableName = "hello";
            this._items.Table.AcceptChanges();
        }

        public string ToXmlString()
        {
            string result;
            using (StringWriter sw = new StringWriter())
            {
                this._items.Table.WriteXml(sw);
                result = sw.ToString();
            }

            return result;
        }

        public XPathDocument ToXPathDocument()
        {
            XPathDocument result;
            using (MemoryStream ms = new MemoryStream())
            {
                this._items.Table.WriteXml(ms);
                ms.Position = 0;
                result = new XPathDocument(ms);
            }
            return result;
        }
    }


    internal class Generate {
        private Dictionary<string, Type> _columns;
        public Dictionary<string, Type> ColumnsAvailable { get { return this._columns; } }

        public List<DataColumn> Columns()
        {
            List<DataColumn> dc = new List<DataColumn>();
            DataColumn dataColumn;

            foreach (KeyValuePair<string, Type> kv in this._columns)
            {
                dataColumn = new DataColumn(kv.Key);
                dataColumn.DataType = kv.Value;
                dc.Add(dataColumn);
            }

            return dc;
        }

        public Generate()
        {
            // constructor
            this._columns = new Dictionary<string, Type>();

            this._columns.Add("ID", typeof(int));
            this._columns.Add("Quantity", typeof(int));
            this._columns.Add("Comment", typeof(string));
            this._columns.Add("Price", typeof(double));
        }        
    }
}
